package by.dvrudenko.avtobaza.services.impl;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.dvrudenko.avtobaza.dataaccess.CarTypeDao;
import by.dvrudenko.avtobaza.datamodel.CarType;
import by.dvrudenko.avtobaza.services.CarTypeService;

@Service
public class CarTypeServiceImpl implements CarTypeService {
	private static final Logger LOGGER = LoggerFactory.getLogger(CarServiceImpl.class);

	@Inject
	private CarTypeDao dao;

	@PostConstruct
	private void init() {
		// Код который будет выполнен после созданиея бина
		// this method will be called by Spring after bean instantiation. Can be
		// used for any initialization process.
		LOGGER.info("Instance of Responses is created. Class is: {}",
				getClass().getName());
	}

	@Override
	public CarType get(Long id) {
		CarType entity = dao.getById(id);
		return entity;
	}

	@Override
	public void saveOrUpdate(CarType carType) {
		if (carType.getId() == null) {
			LOGGER.debug("Save new: {carType}", carType);
			dao.insert(carType);
		} else {
			LOGGER.debug("Update: {carType}", carType);
			dao.update(carType);
		}
	}

	@Override
	public void delete(CarType carType) {
		LOGGER.debug("Remove: {carType}", carType);
		dao.delete(carType.getId());

	}

	@Override
	public void deleteAll() {
		LOGGER.debug("Remove all carType");
		dao.deleteAll();
	}

	@Override
	public List<CarType> getAllCarType() {
		return dao.getAll();
	}

}
